package cn.client.ui.view.main.user.emotionLog

import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.wrapContentHeight
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.material3.TextField
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavController
import cn.client.R
import cn.client.data.remoteData.bean.EmotionLogReq
import cn.client.data.userStateManager
import cn.client.ui.components.BaseTopBar
import cn.client.ui.components.SubmitButton
import cn.client.ui.view.main.mindfulness.HomeSection
import kotlinx.coroutines.launch


// 创建情绪日志
@Composable
fun EmotionLogCreate(
    navController: NavController,
    viewModel: EmotionLogViewModel = hiltViewModel()
){
    var incident: String by remember { mutableStateOf("") }
    var feel: String by remember { mutableStateOf("") }
    var idea: String by remember { mutableStateOf("") }
    var act: String by remember { mutableStateOf("") }
    var ending: String by remember { mutableStateOf("") }
    val coroutineScope = rememberCoroutineScope()
    val userIdState = userStateManager.userId.collectAsState(initial = -1)
    val userId = userIdState.value

    val emotionLogModifier: Modifier = Modifier.fillMaxWidth().wrapContentHeight().padding(5.dp)
    Scaffold(
        topBar = {
            BaseTopBar(
                title = "创建情绪日志",
                onClickArrowBack = {
                    coroutineScope.launch {
                        navController.navigateUp()
                    }
                }
            )
        }
    ) {
        Column(
            modifier = Modifier.padding(it)
        ) {
            HomeSection(title = "事件") {
                Row(
                    horizontalArrangement = Arrangement.Center,
                    modifier = emotionLogModifier
                ) {
                    TextField(
                        value = incident,
                        onValueChange = { str -> incident = str },
                        placeholder = { Text(stringResource(R.string.mindfulness_input_info)) },
                        modifier = emotionLogModifier
                    )
                }
            }
            HomeSection(title = "感受") {
                Row(
                    horizontalArrangement = Arrangement.Center,
                    modifier = emotionLogModifier
                ) {
                    TextField(
                        value = feel,
                        onValueChange = { str -> feel = str },
                        placeholder = { Text(stringResource(R.string.mindfulness_input_info)) },
                        modifier = emotionLogModifier
                    )
                }
            }
            HomeSection(title = "想法") {
                Row(
                    horizontalArrangement = Arrangement.Center,
                    modifier = emotionLogModifier
                ) {
                    TextField(
                        value = idea,
                        onValueChange = { str -> idea = str },
                        placeholder = { Text(stringResource(R.string.mindfulness_input_info)) },
                        modifier = emotionLogModifier
                    )
                }
            }
            HomeSection(title = "行动") {
                Row(
                    horizontalArrangement = Arrangement.Center,
                    modifier = emotionLogModifier
                ) {
                    TextField(
                        value = act,
                        onValueChange = { str -> act = str },
                        placeholder = { Text(stringResource(R.string.mindfulness_input_info)) },
                        modifier = emotionLogModifier
                    )
                }
            }
            HomeSection(title = "结果") {
                Row(
                    horizontalArrangement = Arrangement.Center,
                    modifier = emotionLogModifier
                ) {
                    TextField(
                        value = ending,
                        onValueChange = { str -> ending = str },
                        placeholder = { Text(stringResource(R.string.mindfulness_input_info)) },
                        modifier = emotionLogModifier
                    )
                }
            }
            SubmitButton(
                onClickSubmitButton = {
                    val req = EmotionLogReq(
                        uid = userId,
                        incident = incident,
                        feel = feel,
                        idea = idea,
                        act = act,
                        ending = ending
                    )
                    coroutineScope.launch {
                        viewModel.dispatch(EmotionLogViewModel.PageIntent.Add(req))
                    }
                }
            )
        }
    }

}